package com.example.subject10mvn;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//Excel模板生成器
public class ExcelModelGenerator {
    /*
    @Param excelContent excel中的内容对象，包含字段、提示信息
    @Param dir excel生成的位置
     */
    public String CreateExcelModel(ExcelContent excelContent,String dir) throws IOException {
        //创建工作簿
        XSSFWorkbook workbook=new XSSFWorkbook ();
        //创建表格
        XSSFSheet sheet=workbook.createSheet ("sheet1");
        //创建第一行
        XSSFRow row1= (XSSFRow) sheet.createRow (0);//注意，从0开始

        Font font=workbook.createFont();
        font.setColor (IndexedColors.BLUE.getIndex ());

        XSSFCellStyle style=workbook.createCellStyle();
        style.setFont(font);
        style.setFillForegroundColor(IndexedColors.RED.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);




        //第一行写入数据
        row1.createCell (0).setCellValue ("第一列");//注意，从0开始
        row1.createCell (1).setCellValue ("第二列");
        Cell ceil=row1.createCell (2);
        ceil.setCellValue ("第3列");
        ceil.setCellStyle (style);


        //表格通过流写入到硬盘上
        FileOutputStream fileOutputStream=new FileOutputStream ("C:\\Users\\admin\\Desktop\\新建表格.xlsx");
        workbook.write (fileOutputStream);
        fileOutputStream.flush ();

        //释放资源
        workbook.close ();
        fileOutputStream.close ();
        return "";
    }
    //private
}
